package com.lanqiao.gxnushop.service;

import com.lanqiao.gxnushop.entity.Buyers;
import com.lanqiao.gxnushop.mapper.BuyersMapper;
import com.lanqiao.gxnushop.util.UUIdUtil;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Schema(description = "买家服务")
@Service
public class BuyersService {
    @Autowired
    private BuyersMapper buyersMapper;
    @Autowired
    private UserLoginsService userLoginsService;

    @Schema(description = "增加买家信息")
    public boolean add(Buyers buyers) {
        buyers.setBuyerId(UUIdUtil.getUserId());
        return buyersMapper.add(buyers);
    }

    /**注册时同步 将主键 插入 buyers 表*/
    public boolean addBuyer(String buyerId) {
        return buyersMapper.addBuyer(buyerId);
    }

    @Schema(description = "删除买家信息")
    public boolean delete(String buyerId) {
        if(userLoginsService.findById(buyerId) != null){
            userLoginsService.delete(buyerId);
            return buyersMapper.delete(buyerId);
        }
        else return false;
    }

    @Schema(description = "根据买家id查询买家信息")
    public Buyers findById(String buyerId) {
        return buyersMapper.findById(buyerId);
    }

    @Schema(description ="查询所有买家")
    public ArrayList<Buyers> findAll() {
        return buyersMapper.list();
    }

    @Schema(description ="根据名字模糊查询买家")
    public ArrayList<Buyers> findByName(String name) {
        return buyersMapper.findByName(name);
    }

    @Schema(description = "更新买家信息")
    public boolean update(Buyers buyers) {
        return buyersMapper.update(buyers);
    }

    public boolean updateYhtx(Buyers buyers) {
        return buyersMapper.updateYhtx( buyers);
    }
}
